import sys

from PyQt5 import QtCore
from PyQt5.QtGui import QFont
from PyQt5.QtWidgets import QWidget, QGridLayout, QLineEdit, QPushButton, QMainWindow, QApplication, QLabel, \
    QHBoxLayout, QVBoxLayout, QTextEdit
from PyQt5 import QtMultimedia
from playsound import playsound
from controller.word_controller import get_new_random_word


class MainPage(QMainWindow):
    is_saved = True
    is_saved_first = True
    path = ''

    def __init__(self):
        super(MainPage, self).__init__()
        self.resize(600, 400)  # 通过resize(200, 200)方法来设置窗口大小
        self.move(600, 300)  # 将窗口移到屏幕坐标为(100, 100)的位置上
        self.menu_init()
        self.spelling_label = QLabel('拼写', self)
        self.pronounce_label = QLabel('发音', self)
        self.meaning_label = QLabel('释义', self)
        self.example_label = QLabel('例子', self)
        self.frequency_label = QLabel('频率 *****', self)
        # self.sound = QSound('sound.wav', self)              # pyqt6还未引入QSound
        # self.play_btn = QPushButton('Play Sound', self)
        # self.play_btn.clicked.connect(self.sound.play)
        self.play_button = QPushButton('播放', self)
        self.collect_button = QPushButton('收藏',self)
        self.spell_button = QPushButton("拼写",self)
        self.next_word_button = QPushButton("下一个", self)

        self.grid_layout = QGridLayout()
        self.h_layout = QHBoxLayout()
        self.v_layout = QVBoxLayout()
        self.layout_init()
        self.label_init()
        self.button_init()
        self.set_word_ui("123")

    def label_init(self):
        self.spelling_label.move(50, 50)
        self.spelling_label.resize(400, 60)
        self.spelling_label.setFont(QFont("Roman times", 50))
        self.pronounce_label.move(60, 110)
        self.meaning_label.move(50, 20)
        self.meaning_label.setWordWrap(True)  # Lable的换行
        self.meaning_label.setFont(QFont("Roman times", 15))
        self.meaning_label.resize(300, 300)

        self.example_label.move(50, 100)
        self.example_label.setWordWrap(True)  # Lable的换行
        self.example_label.setFont(QFont("Roman times", 12))
        self.example_label.resize(300, 300)

        self.frequency_label.move(50, 200)
        self.frequency_label.setWordWrap(True)  # Lable的换行
        self.frequency_label.setFont(QFont("Roman times", 10))
        self.frequency_label.resize(300, 300)

    def button_init(self):
        self.play_button.resize(50, 20)
        self.play_button.move(150, 110)

        self.collect_button.move(500,50)
        self.collect_button.resize(100,50)

        self.next_word_button.move(400,200)
        self.next_word_button.resize(100,50)
        self.spell_button.move(400,260)
        self.spell_button.resize(100,50)

        # self.play_button.clicked.connect(self.play())

    def layout_init(self):
        pass
        # self.grid_layout.addWidget(self.spelling_label, 100, 100, 100, 1)
        # self.grid_layout.addWidget(self.meaning_label, 200, 100, 1, 1)
        # self.v_layout.addLayout(self.grid_layout)
        # self.v_layout.addLayout(self.h_layout)

    def menu_init(self):
        self.user_menu = self.menuBar().addMenu('账户')
        self.select_menu = self.menuBar().addMenu('选择词库')
        self.collection_menu = self.menuBar().addMenu('单词收藏本')
        self.record_menu = self.menuBar().addMenu('记忆记录')
        self.select_menu.addAction(self.select_vocabulary())

    def toolbar_init(self):
        self.copy_toolbar = self.addToolBar('Copy')
        # self.file_toolbar = self.addToolBar('File')
        # self.edit_toolbar = self.addToolBar('Edit')

        self.status_bar = self.statusBar()

    def select_vocabulary(self):
        pass

    def set_word_ui(self, user): # 设置音频
        word = get_new_random_word(user)
        self.spelling_label.setText("abdomen")
        self.pronounce_label.setText("ˈæbdəmən")
        self.meaning_label.setText("腹部")
        self.example_label.setText("He was suffering from pains in his abdomen.他感到腹部剧痛。")

    def play(self): # 播放单词音频
        QtMultimedia.QMediaPlayer("http://res.iciba.com/resource/amp3/oxford/0/7a/6a/7a6adf25a5bbc13ad872a76e2cf050ed.mp3")
        # playsound(r"http://dict.youdao.com/dictvoice?type=1&audio=word", block=False)


    def spelling_window(self):
        pass

if __name__ == '__main__':
    app = QApplication(sys.argv)
    mainpage = MainPage()
    mainpage.show()
    sys.exit(app.exec())
